﻿using Autofac;
using Autofac.Integration.WebApi;
using System.Linq;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;

namespace LS.Api
{
    /// <summary>
    /// Ioc 注册
    /// </summary>
    public class IocConfig
    {
        /// <summary>
        /// Ioc反转控制
        /// </summary>
        public static void RegisterDependencies()
        {
            // 注册组件
            var builder = new ContainerBuilder();
            var services = Assembly.Load("LS.Cores");

            //注册controlls实例

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());


            // 扫描assembly中的组件
            builder.RegisterAssemblyTypes(services)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces();

            IContainer container = builder.Build();

            
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        }
    }
}